SpringBoot와 Redis 연결

✒️ 2026-07-02 23:21 내용 수정


실습 참고 자료


SpringBoot에 의존성 추가

<!-- maven 의존성 -->
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>

연결을 위한 테스트 클래스 작성

package myproject.redis.lettuce.string;  
  
import io.lettuce.core.RedisClient;  
import io.lettuce.core.RedisURI;  
import io.lettuce.core.api.StatefulRedisConnection;  
import io.lettuce.core.api.sync.RedisCommands;  
import org.junit.jupiter.api.Test;  
  
public class RedisLettuceString {  
    @Test  
    public void setGet() {  
        // host 주소  
        String host = "localhost";  
  
        // Redis 연결 주소 생성  
        RedisURI redisURI = RedisURI.builder()  
                .withHost(host)  
                .withPort(6379) // 포트 번호. Docker에서 설정한 값과 동일하게 설정  
                .withDatabase(0) // 0 - 15까지 존재  
                .build();  
  
        // Redis 클라이언트 생성  
        RedisClient redisClient = RedisClient.create(redisURI);  
  
        // Connection 연결  
        StatefulRedisConnection<String, String> connection = redisClient.connect();  
  
        // Redis 명령어  
        RedisCommands<String, String> redisCommands = connection.sync();  
          
        // Redis 연결 테스트를 위한 Key-value        
        String key = "lettuce:string";  
        String value = "hello";  
          
        // Redis에 Key-value 저장  
        redisCommands.set(key, value);  
          
        // 저장된 Key로 value 확인  
        String get = redisCommands.get(key);  
        System.out.println("get = " + get);  
        
        // DB 사용이 완료되면 connection을 끊고 Client도 종료한다.
        connection.close();  
		redisClient.shutdown();
    }  
}

springboot_redis 1.png


RedisConnectionFactory로 연결 시

public class testConnect() {
	// Redis 연결 주소 생성  
	RedisURI redisURI = RedisURI.builder()  
			.withHost("localhost")  
			.withPort(6379) // 포트 번호. Docker에서 설정한 값과 동일하게 설정  
			.withDatabase(0) // 0 - 15까지 존재  
			.build();  

	// Redis 클라이언트 생성  
	RedisClient redisClient = RedisClient.create(redisURI);  

	// Connection 연결  
	StatefulRedisConnection<String, String> connection = redisClient.connect();  
	  
	// Redis 명령어  
	RedisCommands<String, String> redisCommands = connection.sync();  
	
	// 이후 데이터 처리 동작 실행
	
	// DB 사용이 완료되면 connection을 끊고 Client도 종료한다.
	connection.close();  
	redisClient.shutdown();
}
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.data.redis.connection.RedisConnectionFactory;  
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;  
import org.springframework.data.redis.core.RedisTemplate;  
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;  
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;  
import org.springframework.data.redis.serializer.StringRedisSerializer;  

@Configuration  
@EnableRedisRepositories  
public class RedisConfig {  
    @Value("${spring.data.redis.host}")  
    private String host;  
  
    @Value("${spring.data.redis.port}")  
    private Integer port;  
  
    @Bean  
    public RedisConnectionFactory connectionFactory() {  
        return new LettuceConnectionFactory(host, port);  
    }  
  
    /**  
     * Redis 고유 자료구조와 기능 제어 시 사용  
     */  
    @Bean  
    public RedisTemplate<String, Object> redisTemplate() {  
        RedisTemplate<String, Object> template = new RedisTemplate<>();  
  
        template.setConnectionFactory(connectionFactory());  
        template.setKeySerializer(new StringRedisSerializer());  
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());  
        template.setHashKeySerializer(new StringRedisSerializer());  
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());  
  
        template.afterPropertiesSet();  
  
        return template;  
    }  
}

Redis Template을 사용할 경우


Redis Repository를 사용할 경우